﻿namespace Genetic {
    using System;
    using System.Diagnostics;
    using System.Collections;

    public class Chromosome<T> : IEnumerable
        where T : struct {

        public static readonly Random Random = new Random();

        public Chromosome(params T[] genes) {
            _genes = genes;
        }

        public Chromosome() {
        }

        public Chromosome(int length) {
            SetLength(length);
        }

        T[] _genes;
        public T[] Genes {
            get {
                if (_genes == null) {
                    return _genes = new T[] { };
                }
                return _genes;
            }
        }

        double _fitness;
        public double Fitness {
            get { 
                return _fitness; 
            }
            set {
                _fitness = value;
            }
        }

        public override string ToString() {
            return _fitness.ToString();
        }

        public void SetLength(int length) {
            if (length < 0) {
                throw new ArgumentOutOfRangeException("length");
            }
            if (_genes != null
                        && length == _genes.Length) {
                return;
            }
            _genes = new T[length];
        }

        IEnumerator IEnumerable.GetEnumerator() {
            T[] genes = Genes;
            Debug.Assert(genes != null, "genes != null");

            return genes.GetEnumerator();
        }
    }
}